﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rankep.FilterableDataGridView;

namespace DiveManager
{
    public partial class ManageGuidesForm : Form
    {
        protected DiveDatabaseEntities diveDatabase;
        protected BindingSource bindingSource;

        // Item Filter
        private FilterItem textboxFilter;

        public ManageGuidesForm()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
            bindingSource = new BindingSource();
        }

        private void ManageGuidesForm_Load(object sender, EventArgs e)
        {
            // Load list
            LoadData();

            // Add Bindings
            txFirstName.DataBindings.Add(new Binding("Text", bindingSource, "FirstName"));
            txLastName.DataBindings.Add(new Binding("Text", bindingSource, "LastName"));
            txPadiNr.DataBindings.Add(new Binding("Text", bindingSource, "PadiID"));

            // Add Filtering
            textboxFilter = new FilterItem("", "FirstName|LastName");
        }

        protected void LoadData()
        {
            IQueryable<DiveGuides> dataQuery = (from a in diveDatabase.DiveGuides
                                                select a);
            
            // Bind Data to BindingSource
            bindingSource.DataSource = dataQuery;

            // Bind BindingSource to List
            GuidesList.DataSource = bindingSource;

            // Hide ID-Columns
            GuidesList.Columns["ID"].Visible = false;
            GuidesList.Columns["RelationGuidesActivities"].Visible = false;

            // Set Column Headers
            GuidesList.Columns["FirstName"].HeaderText = "Vorname";
            GuidesList.Columns["LastName"].HeaderText = "Nachname";
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            // Generate new Object for DB
            DiveGuides newGuide = new DiveGuides();
            newGuide.ID = Guid.NewGuid();
            newGuide.FirstName = "<neuer Eintrag>";

            // Add to DB
            diveDatabase.AddToDiveGuides(newGuide);
            diveDatabase.SaveChanges();

            // Reload List
            LoadData();
        }

        private void deleteToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Den Guide wirklich löschen?", "Rückfrage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                if (bindingSource.Current != null)
                { 
                    DiveGuides diveGuides = bindingSource.Current as DiveGuides;
                    diveDatabase.DeleteObject(diveGuides);
                    diveDatabase.SaveChanges();
                    LoadData();
                }
            }
        }

        private void GuidesList_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            // Save Data on Rowchange
            bindingSource.EndEdit();
            diveDatabase.SaveChanges();
        }

        private void ManageGuidesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Save Data on FormClose
            bindingSource.EndEdit();
            diveDatabase.SaveChanges();
        }

        private void cmdFilter_TextChanged(object sender, EventArgs e)
        {
            //Change the filter Text
            textboxFilter.Filter = cmdFilter.Text;

            GuidesList.BeginFilterUpdate();
            try
            {
                //Add the filter
                GuidesList.Filters.Add(textboxFilter);
            }
            finally
            {
                GuidesList.EndFilterUpdate();
            }
        }

        private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
       
    }
}
